<template>
  <el-container>
    <!-- 头部 -->
    <el-header>
      <div>
        <img src="../assets/sst1.jpg" alt="" />
        <span>收收兔后台管理系统</span>
      </div>
      <el-button type="info" @click="loginout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- 侧边栏 导航区域 -->
        <div class="collapse" @click="IsCollapse">|||</div>
        <!--
            unique-opened 设置只能打开一个侧边栏下拉框
            collapse 是否开启折叠功能
            collapse-transition 是否开启折叠动画
            router 开启vue-router模式，会以index的path进行跳转
         -->
        <el-menu
          :default-active="isMark"
          class="el-menu-vertical-demo"
          background-color="#2c2c2c"
          text-color="#fff"
          active-text-color="#409EFF"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
        >
          <!-- 一级菜单 -->
          <!-- 动态绑定index为唯一字符串，使用拼接字符串的方式得到字符串格式 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menuList"
            :key="item.id"
          >
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconObj[item.id]"></i>
              <!-- 文字 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + menu2List.path"
              v-for="menu2List in item.children"
              :key="menu2List.id"
              @click="navMark('/' + menu2List.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ menu2List.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 内容主题 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      iconObj: {
        125: 'iconfont icon-yonghuguanli',
        103: 'iconfont icon-quanxian',
        101: 'iconfont icon-shangpinguanli',
        102: 'iconfont icon-dingdan',
        145: 'iconfont icon-shuju',
      },
      isCollapse: false,
      isMark: ''
    }
  },
  created() {
    this.getMenuList()
    this.isMark = window.sessionStorage.getItem('navMarkInfo')
  },
  methods: {
    loginout() {
      // 清除token
      window.sessionStorage.clear()
      // 利用编程式导航跳转到登录页
      this.$router.push('/login')
    },
    async getMenuList() {
      const { data } = await this.$http.get('menus')
      console.log(data)
      if (data.meta.status !== 200) return this.$message.error(data.meta.msg)
      this.menuList = data.data
    },
    IsCollapse() {
      this.isCollapse = !this.isCollapse
    },
    navMark(navMarkInfo) {
      // 持久化选择 菜单选项
      // 在点击中传出路径参数，这边接收并存在内存中，在页面加载时取出赋值
      window.sessionStorage.setItem('navMarkInfo', navMarkInfo)
      this.isMark = navMarkInfo
    }
  },
}
</script>

<style lang="less" scoped>
.collapse{
  background-color: #11332e;
  color: #fff;
  text-align: center;
  line-height: 24px;
  cursor: pointer;
  letter-spacing: 0.15em;
}
.el-menu{
  border-right: none;
}
// 设置最外层 100%
.el-container {
  height: 100%;
}
.el-header {
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  background-color: #018c79;
  color: #fff;
  > div {
    font-size: 24px;
    display: flex;
    align-items: center;
    span {
      padding-left: 10px;
    }
    img {
      width: 60px;
      height: 60px;
    }
  }
}
.el-aside {
  background-color: #2c2c2c;
}
.el-main {
  background-color: #d1cece;
}
.iconfont {
  padding-right: 10px;
}
</style>
